package com.ljc.cjgl.controller;

import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.TbTest;
import com.ljc.cjgl.entitiy.Tea;
import com.ljc.cjgl.form.TestForm;
import com.ljc.cjgl.service.TeaService;
import com.ljc.cjgl.service.TestService;

@Controller
@RequestMapping(value="/test")
public class TestController {
	@Autowired
	private TestService service;
	@Autowired
	private TeaService teacherService;
	@RequestMapping("/manage")
	public String manage() {
		return "test/manage";
	}
	
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap map) throws JsonProcessingException {
		TbTest model=new TbTest();
		if(id!=null) {
			model=service.findOne(id);
		}
		//String modelStr=mapper.writeValueAsString(model);
		//System.out.println(modelStr);
		map.put("model", model);
		List<Tea> teachers = teacherService.findAll();
		map.put("teachers",teachers);
		int[] selTea= {1,3,4};
		map.put("selTea", selTea);
		return "test/edit";
	}
	
	
	@RequestMapping("/page")
	@ResponseBody
	public Object page(TablePageable pageable,TestForm form) {
    	PageRequest pageRequest=pageable.bulidPageRequest();
    	String name = form.getName();
    	Integer type = form.getType();
    	String search=form.getSearch();
    	HashSet<Rule> rules=new HashSet<>();
    	if(StringUtils.hasText(name)) {
    		rules.add(Rule.property("name").contain(name));
    	}
    	if(type!=null) {
    		rules.add(Rule.property("type").equal(type));
    	}
    	if(StringUtils.hasText(search)) {
    		HashSet<Rule> rules2=new HashSet<>();
    		rules2.add(Rule.property("name").contain(search));
    		rules2.add(Rule.property("demo").contain(search));
    		rules2.add(Rule.property("xq").contain(search));
    		rules.add(Rule.or(rules2));
    	}
    	Page<TbTest> page=service.findAll(Rule.and(rules).toSpecification(),pageRequest);
    	return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Object save(TestForm form) {
		service.save(form);
		return TableResult.bulidResult(true, "数据保存成功");
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(@RequestBody List<TbTest> tests) {
		if(service.delete(tests)) {
			return TableResult.bulidResult(true, "数据删除成功");
		}else {
			return TableResult.bulidResult(false, "数据删除失败");
		}
	}
}
